<?php

  include_once( '../library/config.php' );
  include_once( LANGUAGE.'language.php' );
  include_once( FROG_HTML.'Form.class.php' );
  include_once( APPLICATION.'User.class.php' );
  include_once( APPLICATION.'Item.class.php' );
  include_once( APPLICATION.'Category.class.php' );
  
  $_page = 'add';
  $_pname = 'Afegir objecte';
  
  if( User::checkLogged() )
  {
    $user = User::findByEmail( $_SESSION['email'] );
    
    if( $user )
    {
    	if( !empty( $_POST['id_category'] ) && !empty( $_POST['name'] ) && !empty( $_POST['description'] ) && !empty( $_POST['points'] ) )
    	{
    		$it = new Item();
    		$it->setId_category( $_POST['id_category'] );
    		$it->setId_user( $user->getId_user() );
    		$it->setName( $_POST['name'] );
    		$it->setDescription( $_POST['description'] );
    		$it->setPoints( $_POST['points'] );
    		if( $it->insert() )
    		  $okmsg = 'Producte afegit!';
    		else
    		  $komsg = 'Problema afegint producte';
    	}
    	
    	$categories = Category::findVisible();
      Form::createTree( $categories, 'id_category', $cattree );
      $selectCats = Form::createSelect( 'id_category', 'name', $cattree, 'id_category', '' );
    	
    	include_once( PUBLIC_TEMPLATES.$_SESSION['langShort'].'/header.tmp.php' );
    	
    	include_once( PUBLIC_TEMPLATES.$_SESSION['langShort'].'/additem.tmp.php' );
    	
    	include_once( PUBLIC_TEMPLATES.$_SESSION['langShort'].'/footer_logged.tmp.php' );
    	
    	include_once( PUBLIC_TEMPLATES.$_SESSION['langShort'].'/footer.tmp.php' );
    }
    else
    {
    	header( 'Location: index.php' );
    } 
  }
  else
    header( 'Location: index.php' );

?>